package com.example.work.check;


import com.example.work.Excel.AssetEntity;
import com.example.work.entiy.DivisionAssetInfoWithIpPorts;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 资产比对结果类
 * 封装数据比对的各种结果
 */
@Data
public class ComparisonResult {

    /**
     * 需要插入的新资产记录列表
     */
    private List<AssetEntity> updatedAssets = new ArrayList<>();




    
    /**
     * 需要插入的新资产记录列表
     */
    private List<DivisionAssetInfoWithIpPorts> insertRecords = new ArrayList<>();
    
    /**
     * 匹配成功的记录数量
     */
    private int matchedCount = 0;
    
    /**
     * Excel中无效的记录列表（格式缺失或资产类型不明）
     */
    private List<InvalidRecord> invalidRecords = new ArrayList<>();

    /**
     * 需要更新的记录列表
     */
    private List<DivisionAssetInfoWithIpPorts> updateRecords = new ArrayList<>();


    /**
     * Excel中的总记录数
     */
    private int totalExcelRecords = 0;
    
    /**
     * 数据库中的总记录数
     */
    private int totalDatabaseRecords = 0;
    
    public ComparisonResult() {}
    
    /**
     * 添加需要插入的记录
     */
    public void addInsertRecord(DivisionAssetInfoWithIpPorts record) {
        this.insertRecords.add(record);
    }
    
    /**
     * 增加匹配成功的记录数量
     */
    public void incrementMatchedCount() {
        this.matchedCount++;
    }
    
    /**
     * 添加无效记录
     */
    public void addInvalidRecord(String reason, Object originalRecord) {
        this.invalidRecords.add(new InvalidRecord(reason, originalRecord));
    }
    
    // Getter和Setter方法
    public List<DivisionAssetInfoWithIpPorts> getInsertRecords() {
        return insertRecords;
    }
    
    public void setInsertRecords(List<DivisionAssetInfoWithIpPorts> insertRecords) {
        this.insertRecords = insertRecords;
    }
    
    public int getMatchedCount() {
        return matchedCount;
    }
    

    
    public void setTotalExcelRecords(int totalExcelRecords) {
        this.totalExcelRecords = totalExcelRecords;
    }
    

    public void setTotalDatabaseRecords(int totalDatabaseRecords) {
        this.totalDatabaseRecords = totalDatabaseRecords;
    }
    
    /**
     * 获取需要插入的记录数量
     */
    public int getInsertCount() {
        return insertRecords.size();
    }

    /**
     * 获取需要更新的记录数量
     */
    public int getUpdateRecordsCount() {
        return updateRecords.size();
    }
    
    /**
     * 获取无效记录数量
     */
    public int getInvalidCount() {
        return invalidRecords.size();
    }

    /**
     * 添加更新记录
     */
    public void addUpdateRecord(DivisionAssetInfoWithIpPorts record) {
        this.updateRecords.add(record);
    }




    /**
     * 生成详细的比对报告
     */
    public String generateDetailedReport() {
        StringBuilder report = new StringBuilder();
        report.append("=== 资产数据比对报告 ===\n");
        report.append("Excel总记录数: ").append(totalExcelRecords).append("\n");
        report.append("数据库总记录数: ").append(totalDatabaseRecords).append("\n");
        report.append("匹配成功记录数: ").append(matchedCount).append("\n");
        report.append("需要插入记录数: ").append(getInsertCount()).append("\n");
        report.append("需要更新记录数: ").append(getUpdateRecordsCount()).append("\n");
        report.append("无效记录数: ").append(getInvalidCount()).append("\n");
        report.append("==================\n\n");
        
        if (!invalidRecords.isEmpty()) {
            report.append("无效记录详情:\n");
            for (int i = 0; i < invalidRecords.size(); i++) {
                InvalidRecord invalid = invalidRecords.get(i);
                report.append(String.format("%d. %s\n", i + 1, invalid.getReason()));
                report.append("   记录: ").append(invalid.getOriginalRecord()).append("\n");
            }
            report.append("\n");
        }
        
        if (!insertRecords.isEmpty()) {
            report.append("需要插入的记录（前5条）:\n");
            for (int i = 0; i < Math.min(5, insertRecords.size()); i++) {
                DivisionAssetInfoWithIpPorts record = insertRecords.get(i);
                report.append(String.format("%d. IP: %s, 端口: %s, 类型: %s, 名称: %s\n",
                        i + 1, record.getAssetIp(), record.getPorts(),
                        record.getReportAssetType(), record.getAssetName()));
            }
            if (insertRecords.size() > 5) {
                report.append("   ... 还有 ").append(insertRecords.size() - 5).append(" 条记录\n");
            }
        }
        
        return report.toString();
    }


    /**
     * 无效记录内部类
     */
    public static class InvalidRecord {
        private String reason;
        private Object originalRecord;
        
        public InvalidRecord(String reason, Object originalRecord) {
            this.reason = reason;
            this.originalRecord = originalRecord;
        }
        
        public String getReason() {
            return reason;
        }
        
        public void setReason(String reason) {
            this.reason = reason;
        }
        
        public Object getOriginalRecord() {
            return originalRecord;
        }
        
        public void setOriginalRecord(Object originalRecord) {
            this.originalRecord = originalRecord;
        }
        
        @Override
        public String toString() {
            return "InvalidRecord{" +
                    "reason='" + reason + '\'' +
                    ", originalRecord=" + originalRecord +
                    '}';
        }
    }
    
    @Override
    public String toString() {
        return "ComparisonResult{" +
                "insertCount=" + getInsertCount() +
                ", matchedCount=" + matchedCount +
                ", invalidCount=" + getInvalidCount() +
                ", totalExcelRecords=" + totalExcelRecords +
                ", totalDatabaseRecords=" + totalDatabaseRecords +
                '}';
    }
}